<?php
class Db extends PDO {
	public $handle = null;
	public $res;
	
	function __construct() {
		try {
			if ($this->handle == null) {
				$this->handle = new PDO('mysql:host=200.98.140.15;dbname=avaliemais', 'root', 'q1w2e3r4');
				$this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
				$this->handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
				return $this->handle;
			}
		} catch (Exception $e ) {
			echo 'Falha ao conectar: ' . $e->getMessage ();
		}
	}	
	function __destruct() {
		$this->handle = NULL;
	}
	public function Select($tabela,$campos,$where,$order){
		try {
			$sql = "SELECT ";
			$total = count($campos);
			for($i = 0;$i < $total;$i++){
				$sql .= $campos[$i];
				if($i < $total-1){
					$sql .= ',';
				}
			}
			$sql .= ' FROM '.$tabela.' WHERE 1=1';
			if($where != ''){
				$sql .= ' AND '.$where;
			}
			if($order != ''){
				$sql .= ' ORDER BY '.$order;
			}
			$con = $this->handle->prepare($sql);
			if(!$con->execute()){
				throw new Exception('Ocorreu um erro ao consultar');
			}
			$this->res = $con->fetchAll();
			$con->closeCursor();
		} catch (Exception $e) {
			$this->res = 'Erro: '.$e->getMessage();
		}		
	}
	public function Insert($tabela,$obj){
		try {
			$class = get_class_methods(get_class($obj));
			foreach(get_class_methods(get_class($obj)) as $val){
				if(substr($val,0,3) == 'get' && $val != 'getId'.$tabela){
					if($obj->$val() != ''){
						$valores = substr($val,3,strlen($val));
						$nomes[] = ':'.strtolower(substr($val,3,strlen($val)));
						$campos[strtolower($valores)] = $obj->$val();
					}
				}
			}
			$total = count($campos);
			$sql = "INSERT INTO ".$tabela." (".implode(',', array_keys($campos)).') VALUES ('.implode(',', array_values($nomes)).')';
			$con = $this->handle->prepare($sql);
			if(!$con->execute($campos)){
				throw new Exception('Ocorreu um erro ao inserir');
			}
		} catch (Exception $e) {			
			$this->res = 'Erro: '.$e;
		}
	}
	public function Update($tabela,$obj){
		try {
			$param = 'getId'.$tabela;
			foreach(get_class_methods(get_class($obj)) as $val){ 
				if(substr($val,0,3) == 'get'){
					if($obj->$val() != ''){
						if($val != $param){
							$valores = substr($val,3,strlen($val));
							$nomes[] = ':'.strtolower(substr($val,3,strlen($val)));
							$campos[strtolower($valores)] = $obj->$val();
						}
					}
				}
			}
			$total = count($campos);
			$sql = "UPDATE ".$tabela." SET ";
			$i = 1;
			foreach($campos as $n => $v){
				$v = str_replace("'","",$v);
				$v = str_replace('"','',$v);				
				$v = "'".$v."'";			
				$sql .= $n."=".$v;
				if($i < $total){
					$sql .= ',';
				}
				$i++;
			}
			$sql .= ' WHERE id'.$tabela.' = '.$obj->$param();
			$con = $this->handle->prepare($sql);
			if(!$con->execute($campos)){
				throw new Exception('Ocorreu um erro ao atualizar');
			}
		} catch (Exception $e) {
			$this->res = 'Erro: '.$e;	
		}
	}
	public function Delete($tabela,$obj){
		try {
			$sql = 'DELETE FROM '.$tabela.' WHERE id'.$tabela.' = '.$obj['id'];
			$con = $this->handle->prepare($sql);
			if(!$con->execute()){
				throw new Exception('Ocorreu um erro ao excluir');
			}
		} catch (Exception $e) {
			$this->res = 'Erro: '.$e->getMessage();
		}
	}
	public function Trigger($sql){
		try {
			$con = $this->handle->prepare($sql);
			if(!$con->execute()){
				throw new Exception('Ocorreu um erro ao executar');
			}
			$this->res = $con->fetchAll();
			$con->closeCursor();
		} catch (Exception $e) {
			$this->res = 'Erro: '.$e->getMessage();
		}
	}
}
?>